package com.xmut.bs.handaccount.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;

import com.xmut.bs.handaccount.GlideApp;
import com.xmut.bs.handaccount.R;
import com.xmut.bs.handaccount.bean.ArtContent;
import com.xmut.bs.handaccount.bmob.User;
import com.xmut.bs.handaccount.util.GlideCircleTransform;
import com.xmut.bs.handaccount.widget.TagTextView;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ItemViewDelegate;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.List;

import cn.bmob.v3.BmobUser;
import cn.jzvd.JZVideoPlayer;
import cn.jzvd.JZVideoPlayerStandard;

/**
 * Author Dollkey
 * Date 2018/4/10 15:26.
 */

public class ArticleAdapter extends MultiItemTypeAdapter<ArtContent> {

    private boolean isFollow;
    private View.OnClickListener mOnFollowClickListener;

    public ArticleAdapter(Context context, List<ArtContent> datas) {
        super(context, datas);
        addItemViewDelegate(new ItemViewDelegate<ArtContent>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_article_text;
            }

            @Override
            public boolean isForViewType(ArtContent item, int position) {
                return item.getComment() == null && item.getArtContentType() != null && item.getArtContentType().getType() == 1;
            }

            @Override
            public void convert(ViewHolder holder, ArtContent t, int position) {
                holder.setText(R.id.text, t.getArtContentType().getContent());
            }
        });
        addItemViewDelegate(new ItemViewDelegate<ArtContent>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_article_img;
            }

            @Override
            public boolean isForViewType(ArtContent item, int position) {
                return item.getComment() == null && item.getArtContentType() != null && item.getArtContentType().getType() == 2;
            }

            @Override
            public void convert(ViewHolder holder, ArtContent t, int position) {
                ImageView view = holder.getView(R.id.img);
                GlideApp.with(view).load(t.getArtContentType().getContent()).fitCenter().into(view);
            }
        });
        addItemViewDelegate(new ItemViewDelegate<ArtContent>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_article_video;
            }

            @Override
            public boolean isForViewType(ArtContent item, int position) {
                return item.getComment() == null && item.getArtContentType() != null && item.getArtContentType().getType() == 3;
            }

            @Override
            public void convert(ViewHolder holder, ArtContent t, int position) {
                JZVideoPlayerStandard view = holder.getView(R.id.video_player);
                view.setUp(t.getArtContentType().getVideoBean().getVideoPath(), JZVideoPlayer.SCREEN_WINDOW_LIST, "");
                GlideApp.with(view.getContext()).load(t.getArtContentType().getContent()).into(view.thumbImageView);
            }
        });
        addItemViewDelegate(new ItemViewDelegate<ArtContent>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_article_title;
            }

            @Override
            public boolean isForViewType(ArtContent item, int position) {
                return item.getArticle() != null && item.getComment() == null && item.getArtContentType() == null;
            }

            @Override
            public void convert(ViewHolder holder, ArtContent t, int position) {
                holder.setText(R.id.title, t.getArticle().getTitle());
                holder.setText(R.id.time, t.getArticle().getCreatedAt());
                holder.setText(R.id.username, t.getArticle().getUser().getNickName());

                TagTextView followTv = holder.getView(R.id.follow_tv);
                followTv.setChecked(!isFollow);
                followTv.setTagText(isFollow ? "已关注" : "+关注");
                followTv.setVisibility(t.getArticle().getUser().getObjectId()
                        .equals(BmobUser.getCurrentUser(User.class).getObjectId()) ?
                        View.GONE : View.VISIBLE);
                followTv.setOnClickListener(mOnFollowClickListener);
                ImageView avatarImg = holder.getView(R.id.avatar_img);
                GlideApp.with(avatarImg).load(t.getArticle().getUser().getAvatar())
                        .placeholder(R.mipmap.ic_head_img).error(R.mipmap.ic_head_img)
                        .transform(new GlideCircleTransform(mContext)).override(60, 60).into(avatarImg);

            }
        });
        addItemViewDelegate(new ItemViewDelegate<ArtContent>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_comment;
            }

            @Override
            public boolean isForViewType(ArtContent item, int position) {
                return item.getComment() != null && item.getArtContentType() == null;
            }

            @Override
            public void convert(ViewHolder holder, ArtContent t, int position) {
                holder.setText(R.id.time, t.getComment().getCreatedAt());
                holder.setText(R.id.content, t.getComment().getComment());
                holder.setText(R.id.username, t.getComment().getUser().getNickName());
                ImageView avatarImg = holder.getView(R.id.avatar_img);

                GlideApp.with(avatarImg).load(t.getComment().getUser().getAvatar())
                        .placeholder(R.mipmap.ic_head_img).error(R.mipmap.ic_head_img)
                        .transform(new GlideCircleTransform(mContext)).override(60, 60).into(avatarImg);
            }
        });
        addItemViewDelegate(new ItemViewDelegate<ArtContent>() {
            @Override
            public int getItemViewLayoutId() {
                return R.layout.item_all_comment;
            }

            @Override
            public boolean isForViewType(ArtContent item, int position) {
                return item.getComment() == null && item.getArtContentType() == null && item.getArticle() == null;
            }

            @Override
            public void convert(ViewHolder holder, ArtContent t, int position) {
            }
        });
    }

    public void setOnFollowClickListener(View.OnClickListener onFollowClickListener) {
        mOnFollowClickListener = onFollowClickListener;
    }

    public void setFollow(int position, boolean follow) {
        isFollow = follow;
        notifyItemChanged(position);
    }

    public boolean isFollow() {
        return isFollow;
    }
}
